﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MotionControl;
using WHW_NetUtility;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.IO;
using HalconDotNet;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
using XXTUserUI;

namespace MainFrame
{
    public partial class GlobalMotion
    {

     
        /// <summary>
        /// 步进或伺服轴回原（不包转盘，转盘是输出口控制的一般电机）
        /// </summary>
        /// <param name="axisNo">轴号</param>
        /// <returns></returns>
        public static bool GoHome(int axisNo)
        {
            try
            {
                Stopwatch axisSW = new Stopwatch();
                GlobalMotion.Card_1.AxisList[axisNo].AxisStop();
                Thread.Sleep(100);

                displalyInfo.displayInfoMain($"{GlobalMotion.Card_1.AxisList[axisNo].m_AxisName}回原开始");
                GlobalMotion.Card_1.AxisList[axisNo].GoHome();
                axisSW.Restart();
                while (true)
                {
                    Thread.Sleep(5);
                    var m_Status = GlobalMotion.Card_1.AxisList[axisNo].m_AxisSts;
                    if (m_Status.HomingFlag && m_Status.Arrive)
                    {
                        displalyInfo.displayInfoMain($"{GlobalMotion.Card_1.AxisList[axisNo].m_AxisName}回原完成");
                        GlobalMotion.Card_1.AxisList[axisNo].AxisStop();
                        return true;
                    }
                    if (axisSW.ElapsedMilliseconds > 20000)
                    {
                        displalyInfo.displayErrorMain($"{GlobalMotion.Card_1.AxisList[axisNo].m_AxisName}回原超时，请重试");
                        GlobalMotion.Card_1.AxisList[axisNo].AxisStop();
                        return false;
                    }

                }
            }
            catch (Exception ex)
            {
                displalyInfo.displayInfoMain($"轴[轴号{axisNo}] 回原异常：{ex}");
                return false;
            }
        }
        /// <summary>
        /// 所有轴回原点[步进或者伺服，接入到控制卡上轴端口的]
        /// </summary>
        /// <param name="axisLength">电机数量</param>
        /// <returns></returns>
        public static bool GoHomeAll(int axisLength)
        {
            try
            {
                Task<bool>[] homeTasks = new Task<bool>[axisLength + 1];
                for (int i = 0; i < axisLength; i++)
                {
                    homeTasks[i] = Task.Run(() =>
                    {
                        return GoHome(i);
                    });
                }


                Task.WaitAll(homeTasks);
                bool allResut = true;
                for (int i = 0; i < axisLength + 1; i++)
                {
                    allResut = allResut && homeTasks[i].Result;
                }
                if (allResut)
                {
                    displalyInfo.displayInfoMain($"所有电机回原成功");
                    return true;
                }
                else
                {
                    displalyInfo.displayInfoMain($"有电机回原失败，请查看提示信息");
                    return false;
                }
            }
            catch (Exception ex)
            {
                displalyInfo.displayInfoMain($"所有电机回原异常：{ex}");
                return false;
            }
        }
        public static int GoHome_Await(int axisNo)
        {
            try
            {

                Stopwatch axisSW = new Stopwatch();
                //      GlobalMotion.Card_1.AxisList[axisNo].AxisStop();
                //  Thread.Sleep(100);

                displalyInfo.displayInfoMain($"{GlobalMotion.Card_1.AxisList[axisNo].m_AxisName}回原开始");
                var curAxis = GlobalMotion.Card_1.AxisList[axisNo];
                curAxis.GoHome();
                axisSW.Restart();
                bool isHaveOrg = true;
                if (curAxis.m_AxisID == (int)e_card1_axis.贴标R1下 || curAxis.m_AxisID == (int)e_card1_axis.贴标R2上 || curAxis.m_AxisID == (int)e_card1_axis.撕膜R1 || curAxis.m_AxisID == (int)e_card1_axis.撕膜R2)//无原点
                {

                    Thread.Sleep(100);
                    isHaveOrg = false;
                }
                while (true)
                {
                    Application.DoEvents();
                    if (curAxis.m_AxisSts.HomingFlag)
                    {
                        while (true)
                        {
                            Application.DoEvents();
                            if ((!curAxis.m_AxisSts.HomingFlag || !isHaveOrg) && curAxis.m_AxisSts.Arrive)
                            {
                                displalyInfo.displayInfoMain($"{curAxis.m_AxisName}回原完成");
                                curAxis.AxisStop();
                                return 1;
                            }
                            if (axisSW.ElapsedMilliseconds > 20000 || GlobalVar.isEstop || GlobalVar.isExit)
                            {
                                displalyInfo.displayErrorMain($"{curAxis.m_AxisName}回原超时，请重试");
                                curAxis.AxisStop();
                                return -1;
                            }
                        }
                    }
                    if (axisSW.ElapsedMilliseconds > 20000 || GlobalVar.isEstop || GlobalVar.isExit)
                    {
                        displalyInfo.displayErrorMain($"{GlobalMotion.Card_1.AxisList[axisNo].m_AxisName}回原超时，请重试");
                        GlobalMotion.Card_1.AxisList[axisNo].AxisStop();
                        return -1;
                    }
                    Thread.Sleep(5);
                }
            }
            catch (Exception ex)
            {
                displalyInfo.displayInfoMain($"轴[轴号{axisNo}] 回原异常：{ex}");
                return -1;
            }
        }
        /// <summary>
        /// 根据运动控制卡添加的轴回原，如果有些轴不需要回原，不能用这个方法 
        /// </summary>
        public static int home_all_continue()
        {
            try
            {
                if (Card_1.AxisList.Count < 1)
                {
                    displalyInfo.displayInfoMain("请先添加轴再回原");
                    return -1;
                }
                displalyInfo.displayInfoMain($"回原开始");


                if (GlobalVar.frmHomeTips == null || GlobalVar.frmHomeTips.IsDisposed)
                {
                    GlobalVar.frmHomeTips = new FormHomeTips();
                }
 
                bool isHomeFlag = true;
                bool isHomingFlag = false;
                var axisSW = new Stopwatch();
                axisSW.Restart();
                isHomingFlag = false;
                for (int m = 0; m < GlobalMotion.Card_1.AxisList.Count; m++)
                {
                    var curAxisStatus = GlobalMotion.Card_1.AxisList[m].m_AxisSts;
                    if (curAxisStatus.Alarm)
                    {
                        if (GlobalVar.IsManThrought)///人工过站不检查其他轴
                        {
                            if (m > (int)e_card1_axis.上层输送右)
                            {
                                break;
                            }
                        }
                        if (DialogResult.Yes == MessageBox.Show($"{GlobalMotion.Card_1.AxisList[m].m_AxisName}报警 是否清除报警？", "询问", MessageBoxButtons.YesNo))
                        {
                            GlobalMotion.Card_1.AxisList[m].AxisClearAlarm();
                            m--;
                        }
                        else
                        {
                            displalyInfo.displayInfoMain($"{GlobalMotion.Card_1.AxisList[m].m_AxisName}报警,复位失败");
                            return -1;
                        }

                    }
                    else if (!curAxisStatus.ServoOn)
                    {
                        if (DialogResult.Yes == MessageBox.Show($"{GlobalMotion.Card_1.AxisList[m].m_AxisName}未使能 复位失败？", "询问", MessageBoxButtons.YesNo))
                        {
                            return -1;
                        }
                    }
                }
                //总气检查
                bool isAllAirOK = GlobalMotion.Input_CardEx2[(int)e_input_extCard2.总气源压力报警];
                GlobalMotion.Card_1.AxisList[(int)e_card1_axis.飞达牵引1].AxisStop();
                GlobalMotion.Card_1.AxisList[(int)e_card1_axis.飞达牵引2].AxisStop();
                if (isAllAirOK)
                {
                    //停止输送
                    var nConveryup = GlobalMotion.ConveryControrl(e_ConveryControl.上层输送停止, e_MachineStatus.无控制);
                    //升降气缸
                    var nConvery1 = GlobalMotion.Step_ConveryUp(e_Step_ConveryUp.一段治具下降, e_MachineStatus.无控制);
                    var nConvery2 = GlobalMotion.Step_ConveryUp(e_Step_ConveryUp.二段治具下降, e_MachineStatus.无控制);
                    GlobalMotion.Step_Labeling(e_Step_Label.吸标NG破气, e_platePro.双平台有, e_MachineStatus.无控制);
                    var nConvery3 = GlobalMotion.Step_ConveryUp(e_Step_ConveryUp.一段治具放行, e_MachineStatus.无控制);
                    var nConvery4 = GlobalMotion.Step_ConveryUp(e_Step_ConveryUp.二段治具放行, e_MachineStatus.无控制);
                    var nConvery5 = GlobalMotion.Step_ConveryUp(e_Step_ConveryUp.入料段治具放行, e_MachineStatus.无控制);

                    bool IsWillHomeStart = false;
                    if (GlobalVar.IsManThrought)
                    {
                        IsWillHomeStart = nConvery1 == 1 && nConvery2 == 1 && nConvery3 == 1 && nConvery4 == 1 && nConvery5 == 1;
                    }
                    else
                    {
                        //贴标升降
                        var IslabelAirOK = GlobalMotion.Step_Labeling(e_Step_Label.取标气缸抬起, e_platePro.双平台有, e_MachineStatus.无控制);
                        var NgPut = GlobalMotion.Step_Labeling(e_Step_Label.NG放工装破真空, e_platePro.双平台有, e_MachineStatus.无控制);
                        IsWillHomeStart = nConvery1 == 1 && nConvery2 == 1 && nConvery3 == 1 && nConvery5 == 1 && nConvery4 == 1 && IslabelAirOK == 1;
                    }
                    if (IsWillHomeStart)
                    {
                        IsLabelHomeOk = false;
                        IsCutHomeOk = false;
                        var nZ = 0;
                        if (GlobalVar.IsManThrought)
                        {
                            displalyInfo.displayInfoMain("人工过站复位完成");
                            return 1;
                        }

                        Task.Run(() => nZ = GlobalMotion.AxisHome((int)e_card1_axis.一段压产品Z));
                        Task.Run(Task_CutHome);//撕膜回原
                        Task.Run(Task_LabelHome);//贴标回原
                        System.Threading.Thread.Sleep(500);
                        Task.Run(Task_ConverHome);//上输送带回原

                        while (!GlobalVar.isExit)
                        {
                            Application.DoEvents();
                            Thread.Sleep(10);
                            isHomeFlag = IsLabelHomeOk && IsCutHomeOk && nZ == 1;
                            if (isHomeFlag)
                            {
                                //var ishave = GlobalMotion.Input_CardEx2[(int)e_input_extCard2.上输送治具检测2] || GlobalMotion.Input_CardEx2[(int)e_input_extCard2.上输送治具检测3];
                                //if (ishave)
                                //{
                                //    var sw_convery = new Stopwatch();
                                //    sw_convery.Restart();
                                //    var axis1 = GlobalMotion.Card_1.AxisList[(int)e_card1_axis.上层输送左];
                                //    var axis2 = GlobalMotion.Card_1.AxisList[(int)e_card1_axis.上层输送右];

                                //    GlobalMotion.ConveryControrl(e_ConveryControl.上层输送反转, e_MachineStatus.无控制);
                                //    while (!GlobalVar.isEstop && !GlobalVar.isExit)
                                //    {
                                //        if (!Input_CardEx2[(int)e_input_extCard2.上输送治具检测2] && !Input_CardEx2[(int)e_input_extCard2.上输送治具检测3])
                                //            break;
                                //        Thread.Sleep(3);
                                //        if (sw_convery.ElapsedMilliseconds > GlobalVar.userConfig.ConveryReset_time)
                                //            break;
                                //    }
                                //    GlobalMotion.ConveryControrl(e_ConveryControl.上层输送停止, e_MachineStatus.无控制);

                                //    GlobalMotion.Step_ConveryUp(e_Step_ConveryUp.一段治具阻挡, e_MachineStatus.无控制);
                                //    GlobalMotion.Step_ConveryUp(e_Step_ConveryUp.二段治具阻挡, e_MachineStatus.无控制);
                                //    axis1.PointMove(-GlobalVar.userConfig.dis_ConveryReset, 0.5, false);
                                //    axis2.PointMove(GlobalVar.userConfig.dis_ConveryReset, 0.5, false);

                                //}
                                if (!Input_CardEx1[(int)e_input_extCard1.平台1吸标NG放料位] || !Input_CardEx1[(int)e_input_extCard1.平台2吸标NG放料位])
                                {
                                    var res = GlobalMotion.Step_Labeling(e_Step_Label.吸标NG旋转到放标位, e_platePro.双平台有, e_MachineStatus.无控制);
                                    if (res == 1)
                                        GlobalMotion.Step_Labeling(e_Step_Label.吸标NG下压, e_platePro.双平台有, e_MachineStatus.无控制);
                                }
                                displalyInfo.displayInfoMain("所有轴回原成功");
                                GlobalVar.isAllHome = true;
                                GlobalVar.frmHomeTips.Close();
                                return 1;
                            }
                            if (axisSW.ElapsedMilliseconds > 60000 || GlobalVar.isEstop || nZ == -1)
                            {
                                GlobalVar.isAllHome = false;
                                displalyInfo.displayErrorMain($"有轴回原失败{IsLabelHomeOk}{IsCutHomeOk}，请查看原因并重试");
                                GlobalVar.frmHomeTips.Close();
                                return -1;
                            }
                        }
                        return -1;
                    }
                    else
                    {
                        MessageBox.Show("气缸原点信号检测超时！");
                        displalyInfo.displayErrorMain($"气缸原点信号检测超时！");
                    }

                    return -1;
                }
                else
                {
                    displalyInfo.displayErrorMain($"总气源压力报警");
                    return -1;
                }

            }
            catch (Exception ex)
            {

                Log4Helper.WriteLog(ex);
                return -1;
            }
            GlobalVar.frmHomeTips.Hide();
        }
        /// <summary>
        /// 回零两个X轴再回零R轴
        /// </summary>
        /// <returns></returns>
        private static int Task_LabelHome()
        {
            try
            {
                //int nLabel0 = 0;
                //int nLabel1 = 0;
                //int nLabel2 = 0;
                ///松贴标Z轴刹车
                GlobalMotion.CardExt_2.setOut((ushort)e_output_extCard2.贴标Z轴刹车, e_out_OnOff.off);
                //贴标Z后回零
                var IsZOk = 0;
                var IsThrowZ = 0;
                Task.Run(() => IsZOk = GlobalMotion.AxisHome((int)e_card1_axis.贴标Z轴));
                Task.Run(() => IsThrowZ = GlobalMotion.AxisHome((int)e_card1_axis.剔除Z轴));
                var NgLabel = GlobalMotion.Step_Labeling(e_Step_Label.吸标NG下压, e_platePro.双平台有, e_MachineStatus.无控制);
                if (NgLabel != 1)
                    return -1;

                Stopwatch swTime = new Stopwatch();
                swTime.Restart();
                bool IsZOk12 = false;
                while (true)
                {
                    System.Threading.Thread.Sleep(5);
                    if (GlobalVar.isEstop || GlobalVar.isExit)
                        break;
                    if (swTime.ElapsedMilliseconds > 20000)
                    {
                        break;
                    }
                    if (IsZOk==1 && IsThrowZ==1)
                    {
                        IsZOk12 = true;
                        break;
                    }
                }

                if (IsZOk12)
                {
                    if (GlobalVar.isEstop || GlobalVar.isExit)
                        return -1;

                    var IsX1 = 0;
                    var IsX2 = 0;
                    var IsY = 0;
                    Task.Run(() => IsY = GlobalMotion.AxisHome((int)e_card1_axis.贴标Y轴));
                    Stopwatch sw = new Stopwatch();
                    sw.Restart();
                    while (!GlobalVar.isEstop && !GlobalVar.isExit)
                    {
                        if (IsY == 1)
                        {

                            break;
                        }

                        if (sw.ElapsedMilliseconds > 35000)
                        {
                            return -1;
                        }
                    }
                    if (GlobalVar.isEstop || GlobalVar.isExit)
                        return -1;
                    //进程1  回零两个X轴再回零R轴
                    //var nCur1 = GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标X1下].m_AxisSts.CurrPosi;
                    //var nCur2 = GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标X2上].m_AxisSts.CurrPosi;

                    //if (nCur1>20)
                    //{
                    //    GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标X1下].PointMove(-20, true);
                    //}
                    //if (nCur2 > 20)
                    //{
                    //    GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标X2上].PointMove(-20, true);
                    //}

                    Task.Run(() => IsX1 = GlobalMotion.AxisHome((int)e_card1_axis.贴标X1下));
                    Task.Run(() => IsX2 = GlobalMotion.AxisHome((int)e_card1_axis.贴标X2上));
                    //System.Threading.Thread.Sleep(500);

                    swTime.Restart();
                    IsZOk12 = false;
                    while (true)
                    {
                        System.Threading.Thread.Sleep(5);
                        if (GlobalVar.isEstop || GlobalVar.isExit)
                            break;
                        if (swTime.ElapsedMilliseconds > 20000)
                        {
                            break;
                        }
                        if (IsX2 == 1 && IsX1 == 1 /*&& IsY == 1*/)
                        {
                            IsZOk12 = true;
                            break;
                        }

                    }


                    if (GlobalVar.isEstop || GlobalVar.isExit || !IsZOk12)
                        return -1;
                    GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标X1下].PointMove( - 50, true);
                    GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标X2上].PointMove(-50, true);
                    var IsR2 = false;
                    Task.Run(() =>
                    {
                        if (GlobalVar.isEstop || GlobalVar.isExit)
                            return;
                        var nCurR2 = GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R2上].m_AxisSts.CurrPosi;
                        if (nCurR2 < 0)
                        {
                            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R2上].PointMove(0, 0.5, true);
                            Stopwatch sw_wait = new Stopwatch();
                            sw_wait.Restart();
                            while (nCurR2 > 0)
                            {
                                if (sw_wait.ElapsedMilliseconds > 5000)
                                {
                                    GlobalMotion.MachineState(e_MachineStatus.异常急停, "贴标R2上回零失败");
                                    return;
                                }
                                Application.DoEvents();
                                Thread.Sleep(10);
                                nCurR2 = GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R2上].m_AxisSts.CurrPosi;
                            }
                        }

                        nCurR2 = GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R2上].m_AxisSts.CurrPosi;
                        GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R2上].PointMove(nCurR2 + 5, true);
                        Application.DoEvents();
                        Thread.Sleep(200);
                        GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R2上].AxisStop();
                        bool  IsR2_buf = GlobalMotion.AxisHome((int)e_card1_axis.贴标R2上) == 1;
                        Thread.Sleep(200);
                        //20241208增加回原点偏移
                        if (IsR2_buf)
                        {
                            IsR2 = GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R2上].PointMove(116.40, true);
                            IsR2 = true;
                        }

                        //nLabel1 = IsR2 ? 1 : -1;
                    });

                    var IsR1 = false;
                    Task.Run(() =>
                    {
                        if (GlobalVar.isEstop || GlobalVar.isExit)
                            return;
                        var nCurR1 = GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R1下].m_AxisSts.CurrPosi;
                        if (nCurR1 > 0)
                        {
                            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R1下].PointMove(0, 0.5, true);
                            Stopwatch sw_wait = new Stopwatch();
                            sw_wait.Restart();
                            while (nCurR1 > 0)
                            {
                                if (sw_wait.ElapsedMilliseconds > 5000)
                                {
                                    GlobalMotion.MachineState(e_MachineStatus.异常急停, "贴标R1下回零失败");
                                    return;
                                }
                                Application.DoEvents();
                                Thread.Sleep(10);
                                nCurR1 = GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R1下].m_AxisSts.CurrPosi;
                            }
                        }
                        GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R1下].PointMove(nCurR1 - 5, true);
                        Application.DoEvents();
                        Thread.Sleep(200);
                        GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R1下].AxisStop();
                        IsR1 = GlobalMotion.AxisHome((int)e_card1_axis.贴标R1下) == 1;
                    });

                    if (GlobalVar.isEstop || GlobalVar.isExit)
                        return -1;
                    {
                        //Stopwatch sw = new Stopwatch();
                        sw.Restart();
                        while (!GlobalVar.isEstop && !GlobalVar.isExit)
                        {
                            if (IsR1 && IsR2)
                            {
                                IsLabelHomeOk = true;
                                break;
                            }

                            if (sw.ElapsedMilliseconds > 35000)
                            {
                                return -1;
                            }
                        }
                    }
                    
                    
                    //Task.Run(() =>
                    //{

                    //    if (GlobalVar.isEstop || GlobalVar.isExit)
                    //        return;
                    //    //var IsX2 = GlobalMotion.AxisHome((int)e_card1_axis.贴标X2上) == 1;

                    //    var IsR2 = false;
                    //    Task.Run(() =>
                    //    {
                    //        if (IsX2)
                    //        {
                    //            if (GlobalVar.isEstop || GlobalVar.isExit)
                    //                return;
                    //            var nCurR2 = GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R2上].m_AxisSts.CurrPosi;
                    //            if (nCurR2 > 0)
                    //            {
                    //                GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R2上].PointMove(0, 0.5, true);
                    //                Stopwatch sw_wait = new Stopwatch();
                    //                sw_wait.Restart();
                    //                while (nCurR2 > 0)
                    //                {
                    //                    if (sw_wait.ElapsedMilliseconds > 5000)
                    //                    {
                    //                        GlobalMotion.MachineState(e_MachineStatus.异常急停, "贴标R2上回零失败");
                    //                        return;
                    //                    }
                    //                    Application.DoEvents();
                    //                    Thread.Sleep(10);
                    //                    nCurR2 = GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R2上].m_AxisSts.CurrPosi;
                    //                }
                    //            }

                    //            nCurR2 = GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R2上].m_AxisSts.CurrPosi;
                    //            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R2上].PointMove(nCurR2 - 5, true);
                    //            Application.DoEvents();
                    //            Thread.Sleep(200);
                    //            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R2上].AxisStop();
                    //            IsR2 = GlobalMotion.AxisHome((int)e_card1_axis.贴标R2上) == 1;
                    //        }
                    //        nLabel1 = IsX2 && IsR2 ? 1 : -1;
                    //    });

                    //    Task.Run(() =>
                    //    {

                    //        if (IsX2)
                    //        {
                    //            if (GlobalVar.isEstop || GlobalVar.isExit)
                    //                return;
                    //            var nCurR1 = GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R1下].m_AxisSts.CurrPosi;
                    //            if (nCurR1 > 0)
                    //            {
                    //                GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R1下].PointMove(0, 0.5, true);
                    //                Stopwatch sw_wait = new Stopwatch();
                    //                sw_wait.Restart();
                    //                while (nCurR1 > 0)
                    //                {
                    //                    if (sw_wait.ElapsedMilliseconds > 5000)
                    //                    {
                    //                        GlobalMotion.MachineState(e_MachineStatus.异常急停, "贴标R1下回零失败");
                    //                        return;
                    //                    }
                    //                    Application.DoEvents();
                    //                    Thread.Sleep(10);
                    //                    nCurR1 = GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R1下].m_AxisSts.CurrPosi;
                    //                }
                    //            }
                    //            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R1下].PointMove(nCurR1 - 5, true);
                    //            Application.DoEvents();
                    //            Thread.Sleep(200);
                    //            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R1下].AxisStop();
                    //            IsR1 = GlobalMotion.AxisHome((int)e_card1_axis.贴标R1下) == 1;
                    //        }

                    //    });


                    //});
                    //if (GlobalVar.isEstop || GlobalVar.isExit)
                    //    return -1;

                    
                    {
                      
                    }
                    
                }

                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        private static bool IsCutHomeOk = false;
        private static bool IsLabelHomeOk = false;
        private static void Task_CutHome()
        {
            //松夹子
            CardExt_2.setOut((int)e_output_extCard2.撕膜气缸1, e_out_OnOff.off);
            CardExt_2.setOut((int)e_output_extCard2.撕膜气缸2, e_out_OnOff.off);
            //撕膜Z回零
            var IsZOk = GlobalMotion.AxisHome((int)e_card1_axis.撕膜Z) == 1;
            if (IsZOk)
            {
                //撕膜X回零0
                var nXOK = 0;
                Task.Run(() => nXOK = GlobalMotion.AxisHome((int)e_card1_axis.撕膜X));
                System.Threading.Thread.Sleep(500);
                //撕膜Y回零
                var nYOK = 0;
                
                //撕膜R1运动+2
                var nR1OK = 0;
                var nCurR1 = Card_1.AxisList[(int)e_card1_axis.撕膜R1].m_AxisSts.CurrPosi;

                //撕膜R1回零
                Task.Run(() =>
                {
                    GlobalMotion.toPosition(nCurR1 + 10, (int)e_card1_axis.撕膜R1, 1, true);
                    nR1OK = AxisHome((int)e_card1_axis.撕膜R1);
                });

                //撕膜R2运动+2
                var nR2OK = 0;
                var nCurR2 = Card_1.AxisList[(int)e_card1_axis.撕膜R2].m_AxisSts.CurrPosi;
                //撕膜R2回零
                Task.Run(() =>
                {
                    GlobalMotion.toPosition(nCurR2 + 10, (int)e_card1_axis.撕膜R2, 1, true);
                    nR2OK = AxisHome((int)e_card1_axis.撕膜R2);
                });

                Stopwatch sw = new Stopwatch();
                sw.Restart();
                while (!GlobalVar.isEstop && !GlobalVar.isExit)
                {
                    if (nR1OK == 1 && nR2OK == 1 && nXOK == 1)
                    {
                        break;
                    }
                    else if ((nR1OK == -1 || nR2OK == -1 || nXOK == -1 ))
                    {
                        GlobalVar.isEstop = true;
                        break;
                    }

                    if (sw.ElapsedMilliseconds > 25000)
                    {
                        GlobalVar.isEstop = true;
                        break;
                    }
                }
               
                if (GlobalVar.isEstop || GlobalVar.isExit)
                    return;
                Task.Run(() => nYOK = GlobalMotion.AxisHome((int)e_card1_axis.撕膜Y));

                sw.Restart();
                while (!GlobalVar.isEstop && !GlobalVar.isExit)
                {
                    if (nR1OK == 1 && nR2OK == 1 && nXOK == 1 && nYOK == 1)
                    {
                        IsCutHomeOk = true;
                        return;
                    }
                    else if ((nR1OK == -1 || nR2OK == -1 || nXOK == -1 || nYOK == -1))
                    {
                        GlobalVar.isEstop = true;
                        return;
                    }

                    if (sw.ElapsedMilliseconds > 25000)
                    {
                        GlobalVar.isEstop = true;
                        return;
                    }
                }
            }



        }

        private static void Task_ConverHome()
        {
            try
            {
                GlobalMotion.ConveryControrl(e_ConveryControl.上层输送反转, e_MachineStatus.无控制);
                var sw_convery = new Stopwatch();
                sw_convery.Restart();
                bool isCy1 = false;
                bool isCy2 = false;
                while (!GlobalVar.isEstop && !GlobalVar.isExit)
                {
                    Thread.Sleep(3);

                    if (!Input_CardEx2[(int)e_input_extCard2.上输送治具检测2] && !isCy1)
                    {
                        isCy1 = true;
                        Task.Run(()=>
                        {
                            GlobalMotion.Step_ConveryUp(e_Step_ConveryUp.一段治具阻挡, e_MachineStatus.无控制);
                        });
                    }
                    if (!Input_CardEx2[(int)e_input_extCard2.上输送治具检测3] && !isCy2)
                    {
                        isCy2 = true;
                        Task.Run(() =>
                        {
                            GlobalMotion.Step_ConveryUp(e_Step_ConveryUp.二段治具阻挡, e_MachineStatus.无控制);
                        });
                        
                    }
                    if (!Input_CardEx2[(int)e_input_extCard2.上输送治具检测2] && !Input_CardEx2[(int)e_input_extCard2.上输送治具检测3])
                    {
                        Task.Run(() =>
                        {
                            GlobalMotion.Step_ConveryUp(e_Step_ConveryUp.一段治具阻挡, e_MachineStatus.无控制);
                        });
                        Task.Run(() =>
                        {
                            GlobalMotion.Step_ConveryUp(e_Step_ConveryUp.二段治具阻挡, e_MachineStatus.无控制);
                        });

                        GlobalMotion.ConveryControrl(e_ConveryControl.上层输送停止, e_MachineStatus.无控制);
                        System.Threading.Thread.Sleep(500);
                        GlobalMotion.ConveryControrl(e_ConveryControl.上层输送启动, e_MachineStatus.无控制);
                        System.Threading.Thread.Sleep(1200);
                        GlobalMotion.ConveryControrl(e_ConveryControl.上层输送停止, e_MachineStatus.无控制);

                        break;
                    }

                    if (sw_convery.ElapsedMilliseconds > 10000)
                    {
                        GlobalMotion.ConveryControrl(e_ConveryControl.上层输送停止, e_MachineStatus.无控制);
                        displalyInfo.displayErrorMain("上层流水线 回零超时！");
                        break;
                    }
                }
            }
            catch (Exception) { }
            GlobalMotion.ConveryControrl(e_ConveryControl.上层输送停止, e_MachineStatus.无控制);
        }
        /// <summary>
        /// 机器是否能动作
        /// </summary>
        public static bool b_canMove
        {
            get
            {
                if (!GlobalVar.isHardStart)
                {
                    displalyInfo.displayInfoMain?.Invoke("运动控制卡没有全部开启");
                    return false;
                }
                //return !GlobalVar.isEstop && !GlobalVar.isAllHome && !GlobalVar.isExit;
                return !GlobalVar.isEstop && !GlobalVar.isExit;
            }
        }
        /// <summary>
        /// 警告提示
        /// </summary>
        /// <param name="warnType"></param>
        /// <param name="warnStr"></param>
        public static void warnningTips(e_warningType warnType, string warnStr)
        {
            GlobalVar.warningTips[(int)warnType].haveInfo = true;
            GlobalVar.warningTips[(int)warnType].Info = warnStr;
            displalyInfo.displayInfoMain(warnStr);
            //GlobalVar.isAutoRun = false;
            //GlobalVar.isStep = false;
        }
        /// <summary>
        /// 错误提示
        /// </summary>
        /// <param name="warnType"></param>
        /// <param name="warnStr"></param>
        public static void errorTips(e_errorType warnType, string warnStr)
        {
            GlobalVar.errorTips[(int)warnType].haveInfo = true;
            GlobalVar.errorTips[(int)warnType].Info = warnStr;
            displalyInfo.displayInfoMain(warnStr);
            GlobalVar.isAutoRun = false;
            GlobalVar.isPause = true;
            GlobalVar.isEstop = true;
            GlobalVar.IsMesEstop = false;
        }















    }

    public class CMachine
    {
        #region instance

        private static CMachine _instance;
        public static CMachine Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new CMachine();
                return _instance;
            }
        }

        #endregion

        public bool IsTestModel { get; set; } = false;

    }

}
